/*
 * Copyright (c) 2022-2023 Termony Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _LOS_ARCH_INTERRUPT_H
#define _LOS_ARCH_INTERRUPT_H

#include "los_error.h"
#include "los_config.h"
#include "los_compiler.h"
#include "los_interrupt.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: Invalid interrupt number.
 *
 * Value: 0x02000900
 *
 * Solution: Ensure that the interrupt number is valid.
 * The value range of the interrupt number applicable for a Cortex-M33 platform is [OS_USER_HWI_MIN,OS_USER_HWI_MAX].
 */
#define OS_ERRNO_HWI_NUM_INVALID              LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x00)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: Null hardware interrupt handling function.
 *
 * Value: 0x02000901
 *
 * Solution: Pass in a valid non-null hardware interrupt handling function.
 */
#define OS_ERRNO_HWI_PROC_FUNC_NULL           LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x01)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: Insufficient interrupt resources for hardware interrupt creation.
 *
 * Value: 0x02000902
 *
 * Solution: Increase the configured maximum number of supported hardware interrupts.
 */
#define OS_ERRNO_HWI_CB_UNAVAILABLE           LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x02)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: Insufficient memory for hardware interrupt initialization.
 *
 * Value: 0x02000903
 *
 * Solution: Expand the configured memory.
 */
#define OS_ERRNO_HWI_NO_MEMORY                LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x03)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: The interrupt has already been created.
 *
 * Value: 0x02000904
 *
 * Solution: Check whether the interrupt specified by the passed-in interrupt number has already been created.
 */
#define OS_ERRNO_HWI_ALREADY_CREATED          LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x04)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: Invalid interrupt priority.
 *
 * Value: 0x02000905
 *
 * Solution: Ensure that the interrupt priority is valid.
 * The value range of the interrupt priority applicable for a Cortex-M33 platform is [0,15].
 */
#define OS_ERRNO_HWI_PRIO_INVALID             LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x05)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: Incorrect interrupt creation mode.
 *
 * Value: 0x02000906
 *
 * Solution: The interrupt creation mode can be only set to OS_HWI_MODE_COMM or
 * OS_HWI_MODE_FAST of which the value can be 0 or 1.
 */
#define OS_ERRNO_HWI_MODE_INVALID             LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x06)

/* *
 * @ingroup los_arch_interrupt
 * Hardware interrupt error code: The interrupt has already been created as a fast interrupt.
 *
 * Value: 0x02000907
 *
 * Solution: Check whether the interrupt specified by the passed-in interrupt number has already been created.
 */
#define OS_ERRNO_HWI_FASTMODE_ALREADY_CREATED LOS_ERRNO_OS_ERROR(LOS_MOD_HWI, 0x07)


/* *
 * @ingroup los_arch_interrupt
 * Maximum number of used hardware interrupts.
 */
#ifndef OS_HWI_MAX_NUM
#define OS_HWI_MAX_NUM                        LOSCFG_PLATFORM_HWI_LIMIT
#endif

/* *
 * @ingroup los_arch_interrupt
 * Highest priority of a hardware interrupt.
 */
#ifndef OS_HWI_PRIO_HIGHEST
#define OS_HWI_PRIO_HIGHEST                   0
#endif

/* *
 * @ingroup los_arch_interrupt
 * Lowest priority of a hardware interrupt.
 */
#ifndef OS_HWI_PRIO_LOWEST
#define OS_HWI_PRIO_LOWEST                    7
#endif

/* *
 * @ingroup los_arch_interrupt
 * Count of armv8 system interrupt vector.
 */
#define OS_SYS_VECTOR_CNT                     0

/* *
 * @ingroup  los_arch_interrupt
 * Define the type of a hardware interrupt vector table function.
 */
typedef VOID (**HWI_VECTOR_FUNC)(void);

/* *
 * @ingroup los_arch_interrupt
 * Count of interrupts.
 */
extern volatile UINTPTR g_intCount;

/* 异常管理初始化. */
extern VOID HalHwiInit(VOID);

/* 异常处理统一入口. */
extern VOID HalInterrupt(UINTPTR hwiIndex);


#define OS_EXC_IN_INIT                  0
#define OS_EXC_IN_TASK                  1
#define OS_EXC_IN_HWI                   2

#define OS_EXC_MODE_USR32               0x10
#define OS_EXC_MODE_FIQ32               0x11
#define OS_EXC_MODE_IRQ32               0x12
#define OS_EXC_MODE_SVC32               0x13
#define OS_EXC_MODE_ABT32               0x17
#define OS_EXC_MODE_UND32               0x1B
#define OS_EXC_MODE_SYS32               0x1F

#define OS_EXC_IMPRECISE_ACCESS_ADDR    0xABABABAB

#define OS_INSTR_SET_MASK               0x01000020U
#define OS_ARM_INSTR_LEN                4
#define OS_THUMB_INSTR_LEN              2


/**
 * @ingroup los_exc
 * ARMv7-A异常类型: 指令未定义异常.
 */
#define OS_EXCEPT_UNDEF_INSTR           1

/**
 * @ingroup los_exc
 * ARMv7-A异常类型: 软件中断异常.
 */
#define OS_EXCEPT_SWI                   2

/**
 * @ingroup los_exc
 * ARMv7-A异常类型: 预取终止异常.
 */
#define OS_EXCEPT_PREFETCH_ABORT        3

/**
 * @ingroup los_exc
 * ARMv7-A异常类型: 数据终止异常.
 */
#define OS_EXCEPT_DATA_ABORT            4


/* ARMv7-A异常的故障状态定义. */
#define OS_FS_BACKGROUND_FAULT          0
#define OS_FS_ALIGNMENT_FAULT           1
#define OS_FS_DEBUG_EVENT               2
#define OS_FS_ACCESS_FLAG_FAULT_1ST     3 /* 0b00011 */
#define OS_FS_ACCESS_FLAG_FAULT_2ND     4 /* 0b00110 */
#define OS_FS_TRANSLATION_FAULT_1ST     5 /* 0b00101 */
#define OS_FS_TRANSLATION_FAULT_2ND     7 /* 0b00111 */
#define OS_FS_SYNC_EXTERNAL_ABORT       8
#define OS_FS_PERMISSION_FAULT          13
#define OS_FS_ASYNC_EXTERNAL_ABORT      22
#define OS_FS_ASYNC_PARITY_ERROR        24
#define OS_FS_SYNC_PARITY_ERROR         25

/* ARMv7-A异常触发时保存的寄存器结构. */
typedef struct TagExcContext {
    UINT32 r0;
    UINT32 r1;
    UINT32 r2;
    UINT32 r3;
    UINT32 r4;
    UINT32 r5;
    UINT32 r6;
    UINT32 r7;
    UINT32 r8;
    UINT32 r9;
    UINT32 r10;
    UINT32 r11;
    UINT32 r12;
    UINT32 sp;
    UINT32 lr;
    UINT32 pc;
    UINT32 spsr;
} EXC_CONTEXT_S;

/* ARMv7-A平台发生异常时保存的异常信息结构. */
typedef struct TagExcInfo {
    /**< 异常发生阶段: 0表示初始化发生异常, 1表示任务发生异常, 2表示中断发生异常. */
    UINT16 phase;
    /**< 异常类型: 当出现异常时, 检查上面列出的数字1 ~ 21. */
    UINT16 type;
    /**< 异常发生时错误的访问地址. */
    UINTPTR faultAddr;
    /**< 中断中发生异常, 指示中断号; 任务中发生异常, 指示任务ID; 如果在初始化过程中发生异常则为0xFFFFFFFF. */
    UINT32 thrdPid;
    /**< 异常嵌套数量. 目前, 仅第一次进入异常时, 支持注册的钩子函数. */
    UINT16 nestCnt;
    /**< 预留, 这里用于记录前一次的异常类型. */
    UINT16 reserved;
    /**< 自动堆栈浮点寄存器发生异常时的硬件上下文. */
    EXC_CONTEXT_S *context;
} ExcInfo;

/* 异常寄存器结构. */
extern EXC_CONTEXT_S g_exc;
/* 异常信息控制块. */;
extern ExcInfo g_excInfo;

#define MAX_INT_INFO_SIZE       (8 + 0x164)

typedef VOID (*EXC_PROC_FUNC)(UINTPTR, EXC_CONTEXT_S *);
VOID HalExcHandleEntry(UINTPTR excType, EXC_CONTEXT_S *excBufAddr, UINTPTR esr, UINTPTR far);

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _LOS_ARCH_INTERRUPT_H */
